<template>
  <div class="home-content">
    <div class="main-nav">
      <div class="nav-item" 
        v-for="(section, index) in mainSections" 
        :key="index"
        :class="{ active: currentSection === section.name }"
        @click="handleNavClick(section.name)"
      >{{ section.name }}</div>
    </div>

    <div v-if="currentSection === '首页'" class="sub-nav">
      <div class="nav-item" 
        v-for="(category, index) in categories" 
        :key="index"
        :class="{ active: currentCategory === category.name }"
        @click="currentCategory = category.name"
      >{{ category.name }}</div>
    </div>

    <div v-if="currentSection === '首页'">
      <!-- 删除轮播图相关代码 -->
      <div class="content-container">
        <div class="course-grid">
          <div class="course-card" 
            v-for="(course, index) in currentCourses" 
            :key="index"
            @click="handleCourseClick(course)"
          >
            <div class="course-image">
              <img :src="course.image" :alt="course.title">
            </div>
            <div class="course-info">
              <h3 class="course-title">{{ course.title }}</h3>
              <p class="instructor">{{ course.instructor }}</p>
            </div>
          </div>
        </div>
      </div>

          <!-- 个性化推荐区 -->
    <div v-if="personalRecommendCourses.length" class="recommend-section">
      <h2 class="section-title">个性化课程推荐</h2>
      <div class="recommend-course-grid">
        <div
          class="recommend-course-card"
          v-for="course in personalRecommendCourses"
          :key="course.id"
          @click="handleCourseClick(course)"
        >
          <div class="recommend-course-image">
            <img :src="course.cover" :alt="course.name" />
          </div>
          <div class="recommend-course-info">
            <h3 class="recommend-course-title">{{ course.name }}</h3>
            <p class="recommend-instructor">{{ course.instructor }}</p>
            <p class="recommend-description">{{ course.description }}</p>
          </div>
        </div>
      </div>
    </div>

      <div class="focus-section">
        <h2 class="focus-title">聚焦个人目标的学习体验</h2>
        <div class="path-card" @click="handlePathClick">
          <div class="path-content">
            <h3>个性化学习路径</h3>
            <p>基于你的学习目标和进度，智能推荐最适合的课程组合</p>
          </div>
        </div>
      </div>

      <div class="focus-section">
        <h2 class="focus-title">热门分类</h2>
        <div class="category-grid">
          <div class="category-card" v-for="(cat, idx) in hotCategories" :key="idx" @click="handleCourseClick(cat)">
            <img :src="cat.cover" :alt="cat.name">
            <div class="category-info">
              <h3>{{ cat.name }}</h3>
              <p>{{ cat.courseCount }} 课程</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="content-container">
      <div class="my-learning">
        <!-- 这里可以添加"我的学习"页面的内容 -->
      </div>
    </div>
  </div>

  <!-- 添加页面底部 -->
  <footer class="footer">
    <div class="footer-content">
      <div class="footer-section">
        <h3>关于我们</h3>
        <ul>
          <li><a href="#">公司简介</a></li>
          <li><a href="#">联系我们</a></li>
          <li><a href="#">加入我们</a></li>
          <li><a href="#">帮助中心</a></li>
        </ul>
      </div>
      <div class="footer-section">
        <h3>探索</h3>
        <ul>
          <li><a href="#">解决方案</a></li>
          <li><a href="#">热门分类</a></li>
          <li><a href="#">学习路径</a></li>
          <li><a href="#">帮助和支持</a></li>
        </ul>
      </div>
      <div class="footer-section">
        <h3>法律</h3>
        <ul>
          <li><a href="#">服务条款</a></li>
          <li><a href="#">隐私政策</a></li>
          <li><a href="#">版权声明</a></li>
          <li><a href="#">免责声明</a></li>
        </ul>
      </div>
    </div>
    <div class="footer-bottom">
      <p>© 2025 学了喵 版权所有</p>
    </div>
  </footer>
</template>

<script>
import axios from 'axios';

export default {
  name: 'HomeContent',
  data() {
    return {
      personalRecommendCourses: [],
      currentSection: '首页',
      mainSections: [
        { name: '首页' },
        { name: '仓颉OJ' },
        { name: '我的学习' }
      ],
      currentCategory: '',
      categories: [],
      recommendCourses: [],
      // 删除轮播图数据
      // banners: [...],
      // currentBannerIndex: 0,
      algorithmCourses: [
        {
          title: '算法设计与分析',
          instructor: '张教授',
          rating: '4.7',
          reviews: '164',
          image: require('@/assets/courses/1.jpg')
        },
        {
          title: '数据结构与算法',
          instructor: '李教授',
          rating: '4.6',
          reviews: '173',
          image: require('@/assets/courses/2.jpg')
        },
        {
          title: '大数据算法',
          instructor: '王教授',
          rating: '4.4',
          reviews: '44',
          image: require('@/assets/courses/3.jpg')
        },
        {
          title: '程序设计与算法',
          instructor: '刘教授',
          rating: '4.8',
          reviews: '19',
          image: require('@/assets/courses/4.jpg')
        }
      ],
      pythonCourses: [
        {
          title: 'Python基础教程',
          instructor: '王教授',
          rating: '4.8',
          reviews: '245',
          image: require('@/assets/courses/1.jpg')
        },
        {
          title: 'Python高级编程',
          instructor: '李教授',
          rating: '4.7',
          reviews: '189',
          image: require('@/assets/courses/2.jpg')
        },
        {
          title: 'Python数据分析',
          instructor: '张教授',
          rating: '4.6',
          reviews: '156',
          image: require('@/assets/courses/3.jpg')
        },
        {
          title: 'Python爬虫技术',
          instructor: '刘教授',
          rating: '4.5',
          reviews: '132',
          image: require('@/assets/courses/4.jpg')
        }
      ],
      bigDataCourses: [
        {
          title: '大数据处理技术',
          instructor: '张教授',
          rating: '4.7',
          reviews: '198',
          image: require('@/assets/courses/1.jpg')
        },
        {
          title: '数据仓库与挖掘',
          instructor: '王教授',
          rating: '4.6',
          reviews: '167',
          image: require('@/assets/courses/2.jpg')
        },
        {
          title: 'Hadoop生态系统',
          instructor: '李教授',
          rating: '4.5',
          reviews: '145',
          image: require('@/assets/courses/3.jpg')
        },
        {
          title: '实时流处理',
          instructor: '刘教授',
          rating: '4.8',
          reviews: '178',
          image: require('@/assets/courses/4.jpg')
        }
      ],
      hotCategories: []
    }
  },
  created() {
    this.fetchCategories();
    this.fetchRecommendCourses();
    this.fetchHotCategories();
    this.fetchPersonalRecommendCourses();
  },
  computed: {
    currentCourses() {
      return this.recommendCourses;
    }
  },
  methods: {
    async fetchCategories() {
      try {
        const response = await axios.get('/api/home/categories');
        this.categories = response.data;
        if (this.categories.length > 0) {
          this.currentCategory = this.categories[0].name;
        }
      } catch (error) {
        console.error('Failed to fetch categories:', error);
      }
    },
    async fetchRecommendCourses() {
      try {
        const response = await axios.get('/api/home/recommend-courses');
        this.recommendCourses = response.data.map(item => ({
          id: item.id,
          title: item.name,
          instructor: item.desc,
          image: item.cover
        }));
      } catch (error) {
        console.error('Failed to fetch recommend courses:', error);
      }
    },

    async fetchHotCategories() {
      try {
        const response = await axios.get('/api/home/hot-categories');
        this.hotCategories = response.data;
      } catch (error) {
        console.error('Failed to fetch hot categories:', error);
      }
    },

    // 获取个性化推荐课程
    async fetchPersonalRecommendCourses() {
      const userId = localStorage.getItem('userId') || 1; // 真实项目用实际登录用户ID
      try {
        const res = await axios.get(`/api/recommend/personal-courses?userId=${userId}`);
        this.personalRecommendCourses = res.data;
      } catch (e) {
        console.error('获取推荐课程失败', e);
      }
    },

    handleNavClick(sectionName) {
      if (sectionName === '我的学习') {
        this.$router.push('/my-learning');
      } else if (sectionName === '仓颉OJ') {
        this.$router.push('/cangjie-oj');
      } else {
        this.currentSection = sectionName;
      }
    },
    handlePathClick() {
      this.$router.push('/learning-path');
    },

    handleCourseClick(course) {
      // 判断是否为热门分类卡片（有 courseCount 字段）
      if (course.courseCount !== undefined) {
        this.$router.push({
          path: '/category-detail',
          query: { name: course.name }
        });
      } else {
        this.$router.push({
          path: '/course',
          query: { 
            id: course.id
          }
        });
      }
    }
  }
}
</script>

<style scoped>
.course-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
  padding: 0;
}

.content-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  min-height: 500px;
  padding: 24px 0;
  margin: 0;
}

.course-image img {
  width: 100%;
  height: 180px;
  object-fit: cover;
  border-radius: 8px 8px 0 0;
}

.course-card {
  border: 1px solid #6d28d2;  /* 修改边框颜色为主题色 */
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  background-color: white;
}

.course-card:hover {
  border-color: #6d28d2;
  background-color: #f8f7fa;
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(109, 40, 210, 0.15);
}

.course-info {
  padding: 12px;
  text-align: left;
}

.course-title {
  font-size: 16px;
  color: #1f2937;
  margin: 0 0 8px 0;
  font-weight: 500;
  line-height: 1.4;
}

.instructor {
  color: #6b7280;
  font-size: 14px;
  margin: 0;
}

.course-image {
  padding: 0;
  margin: 0;
  line-height: 0;
}

.home-content {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 40px;
  margin-top: 20px;
}

.sub-nav {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  padding: 0 20px;
}

.nav-item {
  padding: 8px 24px;
  background-color: white;
  border-radius: 20px;
  cursor: pointer;
  color: #666;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  font-size: 15px;
  font-weight: 500;
}

.nav-item:hover {
  background-color: #f3f4f6;
  color: #6d28d2;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.nav-item.active {
  background-color: #6d28d2;
  color: white;
  box-shadow: 0 4px 12px rgba(109, 40, 210, 0.3);
}
</style>

<style scoped>
.main-nav {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #e5e7eb;
  padding: 0 20px;
}

.main-nav .nav-item {
  padding: 12px 24px;
  background-color: transparent;
  border-radius: 0;
  cursor: pointer;
  color: #666;
  transition: all 0.3s ease;
  box-shadow: none;
  font-size: 16px;
  font-weight: 500;
  position: relative;
}

.main-nav .nav-item.active {
  background-color: transparent;
  color: #6d28d2;
  box-shadow: none;
}

.main-nav .nav-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #6d28d2;
}

.main-nav .nav-item:hover {
  background-color: transparent;
  color: #6d28d2;
}

.focus-section {
  margin-top: 40px;
  margin-left: 0;
  margin-right: 0;
  padding-left: 0;
  padding-right: 0;
}

.focus-title {
  font-size: 28px;
  color: #6d28d2;
  margin: 40px 0 24px 0;
  font-weight: 600;
}

.path-card {
  margin: 0;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.path-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.path-content {
  padding: 32px;
  text-align: left;
}

.path-content h3 {
  font-size: 20px;
  color: #1f2937;
  margin-bottom: 12px;
}

.path-content p {
  color: #6b7280;
  line-height: 1.6;
  margin: 0;
}

/* 主导航样式 */
.main-nav {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #e5e7eb;
  padding: 0 20px;
}

.main-nav .nav-item {
  padding: 12px 24px;
  background-color: transparent;
  border-radius: 0;
  cursor: pointer;
  color: #666;
  transition: all 0.3s ease;
  box-shadow: none;
  font-size: 16px;
  font-weight: 500;
  position: relative;
}

.main-nav .nav-item.active {
  background-color: transparent;
  color: #6d28d2;
  box-shadow: none;
}

.main-nav .nav-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #6d28d2;
}

.main-nav .nav-item:hover {
  background-color: transparent;
  color: #6d28d2;
}

.start-btn {
  padding: 12px 32px;
  background-color: #6d28d2;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.start-btn:hover {
  background-color: #5b21b6;
  transform: translateY(-2px);
}

.path-image {
  width: 300px;
  height: 200px;
  padding: 20px;
}

/* 修复样式 */
.path-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}
</style>

<style scoped>
.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 32px;
  margin: 0;
  padding: 0;
}

.category-card {
  display: flex;
  flex-direction: row;
  align-items: center;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.2s;
  height: 80px;
  padding: 0;
  overflow: hidden;
}
.category-card:hover {
  box-shadow: 0 8px 24px rgba(109,40,210,0.13);
  transform: translateY(-2px);
}
.category-card img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 6px 0 0 6px;
  flex-shrink: 0;
}
.category-info {
  flex: 1;
  padding: 0 0 0 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: left;
}
.category-info h3 {
  margin: 0 0 10px 0;
  font-size: 20px;
  font-weight: 700;
  color: #222;
}
.category-info p {
  margin: 0;
  font-size: 15px;
  color: #6b7280;
  font-weight: 500;
}
</style>

<!-- 添加底部样式 -->
<style scoped>
.footer {
  background-color: #6d28d2;  /* 使用主题色作为背景 */
  color: white;
  padding: 60px 0 20px;
  margin-top: 60px;
  width: 100%;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.footer-section {
  width: 22%;
}

.footer-section h3 {
  font-size: 18px;
  margin-bottom: 20px;
  font-weight: 600;
  color: white;
}

.footer-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.footer-section ul li {
  margin-bottom: 10px;
}

.footer-section ul li a {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: color 0.3s ease;
  font-size: 14px;
}

.footer-section ul li a:hover {
  color: white;
  text-decoration: underline;
}

.social-links {
  display: flex;
  gap: 15px;
}

.social-link {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: color 0.3s ease;
  font-size: 14px;
}

.social-link:hover {
  color: white;
  text-decoration: underline;
}

.footer-bottom {
  text-align: center;
  padding-top: 30px;
  margin-top: 40px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

</style>


<!-- 个性化课程推荐部分 -->
<style scoped>
.recommend-section {
  margin-bottom: 40px;
  margin-left: 0;
  margin-right: 0;
}
.section-title {
  font-size: 28px;
  color: #6d28d2;
  margin: 40px 0 24px 0;
  font-weight: 600;
}
.recommend-course-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* 改为2列，卡片更宽 */
  gap: 40px;
  margin: 0;
}
.recommend-course-card {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 4px 24px rgba(109,40,210,0.08);
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.2s;
  min-height: 140px;
  padding: 0;
  overflow: hidden;
  max-width: 700px;
}
.recommend-course-card:hover {
  box-shadow: 0 8px 32px rgba(109,40,210,0.13);
  transform: translateY(-2px);
}
.recommend-course-image img {
  width: 140px; /* 缩小图片宽度 */
  height: 140px;
  object-fit: cover;
  border-radius: 14px 0 0 14px;
  flex-shrink: 0;
}
.recommend-course-info {
  flex: 1;
  padding: 24px 32px 24px 32px; /* 增加左右padding */
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: left;
  min-width: 0;
}
.recommend-course-title {
  margin: 0 0 10px 0;
  font-size: 22px;
  font-weight: 700;
  color: #222;
  line-height: 1.3;
  word-break: break-word;
}
.recommend-instructor {
  color: #6d28d2;
  font-size: 15px;
  margin: 0 0 8px 0;
  font-weight: 500;
}
.recommend-description {
  color: #444;
  font-size: 15px;
  margin: 0;
  line-height: 1.7;
  word-break: break-all;
  white-space: normal;
}
</style>